package it.secse.idmadaption.plugin.util;

import java.security.cert.X509Certificate;

import org.apache.axis2.client.Options;
import org.tempuri.CredentialProviderServiceStub;
import org.tempuri.CredentialProviderServiceStub.AliasDelegatee_type0;
import org.tempuri.CredentialProviderServiceStub.DelegateeWrapperType;
import org.tempuri.CredentialProviderServiceStub.DelegateeWrapperTypeChoice_type0;
import org.tempuri.CredentialProviderServiceStub.GetSecurityTokenForDelegatee;
import org.tempuri.CredentialProviderServiceStub.GetSecurityTokenForDelegateeResponse;

public class DelegationFrameworkWrapper implements DelegationFrameworkWrapperIF {

	
	public GetSecurityTokenForDelegateeResponse delegate(String serviceAddress, String delegateeAlias,
			String delegator,
			String callbackAddress) {
		
           	try{
            
            CredentialProviderServiceStub client = new CredentialProviderServiceStub();
        	// increase timeout
        	Options opts= client._getServiceClient().getOptions();
        	opts.setTimeOutInMilliSeconds(300000);
        	    	
        	// create security token request & set callback data
        	GetSecurityTokenForDelegatee token = new CredentialProviderServiceStub.GetSecurityTokenForDelegatee();    	
        	
			token.setCallbackAddress(callbackAddress);
			token.setServiceAddress(serviceAddress);
			
			DelegateeWrapperType delegatee = new DelegateeWrapperType();
			DelegateeWrapperTypeChoice_type0 choice = new DelegateeWrapperTypeChoice_type0();
			AliasDelegatee_type0 alias = new AliasDelegatee_type0();
			alias.setAlias(delegateeAlias);
			choice.setAliasDelegatee(alias);
			delegatee.setDelegateeWrapperTypeChoice_type0(choice);
			token.setDelegatee(delegatee);
			
//			token1.setDelegatee("secsecalendar@gmail.com");
			token.setDelegator(delegator);
						       	
        	// get security token
        	GetSecurityTokenForDelegateeResponse response = client.GetSecurityTokenForDelegatee(token);
        	CredentialProviderServiceStub.UsernameTokenType us = response.getGetSecurityTokenForDelegateeResult().getUsernameToken();
        	System.out.println(us.getUsername());
           	
        	return response;
           	
           	} catch(java.lang.Exception e) {
           		System.err.println("DELEGA GIA IN ESSERE");
           	}
 
		return null;
		
		
	}

	

}
